from django.db import models
from django.contrib.auth.models import User


class TodoTask(models.Model):
    task_name = models.CharField(verbose_name='任务名称', max_length=200)
    is_all_done = models.BooleanField(verbose_name='全部完成', default=False)
    author = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        ordering = ['-id']
        verbose_name = "任务"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.task_name


class TodoItem(models.Model):
    content = models.CharField(verbose_name='待办内容', max_length=200)
    is_done = models.BooleanField(verbose_name='是否完成', default=False)
    todo_task = models.ForeignKey(TodoTask, on_delete=models.CASCADE)

    class Meta:
        verbose_name = "待办项"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.content


class ShareList(models.Model):
    """
    共享清单 存储可共享的人
    """
    user = models.ForeignKey(User, verbose_name='可编辑的用户',
                             null=False, on_delete=models.CASCADE, related_name='share_list')
    task = models.ForeignKey(
        TodoTask, verbose_name='可编辑的任务', null=False, on_delete=models.CASCADE, related_name='share_list')
    cancelled = models.BooleanField(
        verbose_name='是否已取消共享', null=False, default=False)
    create_at = models.DateTimeField(
        verbose_name="同意共享编辑时间", null=False, auto_now_add=True, blank=True)

    class Meta:
        verbose_name = "任务共享用户"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.username


class ShareLink(models.Model):
    """
    共享连接 把链接发送给好友 好友点击进去同意即可共同编辑清单
    """
    task = models.ForeignKey(TodoTask, verbose_name="任务",
                             null=False, on_delete=models.CASCADE)
    expires_after = models.IntegerField(
        verbose_name="过期单位 单位小时", null=False, default=7 * 24)
    expires_at = models.DateTimeField(
        verbose_name="过期时间", null=False, default=7 * 24)
    create_at = models.DateTimeField(
        verbose_name="创建共享链接时间", null=False, auto_now_add=True, blank=True)

    class Meta:
        verbose_name = "任务共享链接"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.task.task_name


class TodoCategory(models.Model):
    """
    待办分类
    """

    name = models.CharField(max_length=12, verbose_name="待办分类名称", null=False)
    sort = models.IntegerField(verbose_name="排序值", null=False, default=19)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

    class Meta:
        verbose_name = "待办分类"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class TaskInCategory(models.Model):
    """
    待办和分类关系
    """

    task = models.ForeignKey(TodoTask, on_delete=models.CASCADE)
    category = models.ForeignKey(TodoCategory, on_delete=models.CASCADE)

    class Meta:
        verbose_name = "待办和分类关系"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "待办和分类关系"
